package com.frame.base.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.frame.base.R;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getRes());
        ButterKnife.bind(this);

        initViews();
        initDatas();
    }

    protected abstract int getRes();

    protected abstract void initViews();

    protected abstract void initDatas();

    /**
     * 设置头布局
     */
    protected void initTitle(int resLeft, String strLeft, String strContent, int resContent, String strRight, int resRight) {
        ImageView ivTitleLeft = (ImageView) findViewById(R.id.iv_topbar_left);
        ImageView ivTitleCenter = (ImageView) findViewById(R.id.iv_topbar_logo);
        ImageView ivTitleRight = (ImageView) findViewById(R.id.iv_topbar_right);
        TextView tvTitleLeft = (TextView) findViewById(R.id.tv_topbar_left);
        TextView tvTitleContent = (TextView) findViewById(R.id.tv_topbar_center);
        TextView tvTitleRight = (TextView) findViewById(R.id.tv_topbar_right);
        RelativeLayout rlTitleLeft = (RelativeLayout) findViewById(R.id.rl_title_left);
        RelativeLayout rlTitleRight = (RelativeLayout) findViewById(R.id.rl_title_right);
        rlTitleLeft.setOnClickListener(this);
        rlTitleRight.setOnClickListener(this);


        ivTitleLeft.setVisibility(resLeft == 0 ? View.GONE : View.VISIBLE);
        ivTitleCenter.setVisibility(resContent == 0 ? View.GONE : View.VISIBLE);
        ivTitleRight.setVisibility(resRight == 0 ? View.GONE : View.VISIBLE);
        tvTitleLeft.setVisibility(strLeft == null ? View.GONE : View.VISIBLE);
        tvTitleContent.setVisibility(strContent == null ? View.GONE : View.VISIBLE);
        tvTitleRight.setVisibility(strRight == null ? View.GONE : View.VISIBLE);
        if (resLeft != 0) {
            ivTitleLeft.setImageResource(resLeft);
        }
        if (resContent != 0) {
            ivTitleCenter.setImageResource(resContent);
        }
        if (resRight != 0) {
            ivTitleRight.setImageResource(resRight);
        }


        if (strLeft != null) {
            tvTitleLeft.setText(strLeft);
        }
        if (strContent != null) {
            tvTitleContent.setText(strContent);
        }
        if (strRight != null) {
            tvTitleRight.setText(strRight);
        }
    }

    protected void onClickTitleLeft(View v) {
        finish();
    }

    protected void onClickTitleRight(View v) {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_title_left:
                onClickTitleLeft(view);
                break;
            case R.id.rl_title_right:
                onClickTitleRight(view);
                break;
        }
    }
}
